package com.newworld.dmsapp2.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.TextUtils;

public class StringUtil {
	
	/**
	 * 判断字符串是否为空，true:不为空
	 * @param str
	 * @return
	 */
	public static boolean isStringAvailable(String str){
		boolean flag = false;
		if(null != str && !str.equals("")){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 判断两个字符串是否相等
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean isStringEqual(String str1,String str2){
		boolean flag = false;
		if(null != str1 && null != str2){
			if(str1.equals(str2)){
				flag = true;
			}
		}
		return flag;
	}
	/**
	 * 根据指定的字符把源字符串分割成一个数组
	 * 
	 * @param src
	 * @return
	 */
	public static List<String> splitString(String symbol, String src) {

		if (src == null)
			return null;
		List<String> list = new ArrayList<String>();
		String[] result = src.split(symbol);
		for (int i = 0; i < result.length; i++) {
			list.add(result[i]);
		}
		return list;
	}

	/**
	 * 把list用给定的符号symbol连接成一个字符串
	 * 
	 * @param array
	 * @param symbol
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String joinString(List array, String symbol) {
		String result = "";
		if (array != null) {
			for (int i = 0; i < array.size(); i++) {
				String temp = array.get(i).toString();
				if (temp != null && temp.trim().length() > 0)
					result += (temp + symbol);
			}
			if (result.length() > 1)
				result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	/**
	 * 把string[]用给定的符号symbol连接成一个字符串
	 * 
	 * @param array
	 * @param symbol
	 * @return
	 */
	public static String joinString(String[] array, String symbol) {
		String result = "";
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				String temp = array[i];
				if (temp != null && temp.trim().length() > 0)
					result += (temp + symbol);
			}
			if (result.length() > 1)
				result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	/**
	 * 判断字符串长度是否在一定区间内
	 * 
	 * @param str
	 * @param min
	 * @param max
	 * @return
	 */
	public static boolean isLengthBetween(String str, int min, int max) {
		return !TextUtils.isEmpty(str) && str.length() >= min
				&& str.length() <= max;
	}

	/**
	 * 手机号码匹配
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isMobile(String mobile) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(14[5,7])|(15[^4,\\D])|(17[6-8])|(18[0-9]))\\d{8}$");
		Matcher matcher = p.matcher(mobile);
		return matcher.matches();
	}

	/**
	 * 
	 * @方法名：isContent
	 * @描述：匹配数字、字母、下划线，长度在指定范围10内
	 * @param content
	 * @return
	 * 
	 */
	public static boolean isContent(String content) {
		Pattern pattern = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_]{0,10}+$");
		Matcher matcher = pattern.matcher(content.trim());
		return matcher.matches();
	}
	
	/**
	 * 判断字符串1是否包含字符串2
	 * @param firstStr
	 * @param secondStr
	 * @return
	 */
	public static boolean isStringContaind(String firstStr,String secondStr){
		boolean flag = false;
		if(null != firstStr && null != secondStr){
			if(firstStr.contains(secondStr)){
				flag = true;
			}
		}
		return flag;
	}
	
	/**
	 * 字符串部分替换
	 * @param fullString
	 * @param oldChar
	 * @param newChar
	 * @return
	 */
	public static String replaceString(String fullString,String oldChar,String newChar){
		if(null != fullString && fullString.contains(oldChar)){
			fullString = fullString.replace(oldChar, newChar);
		}
		return fullString;
	}

}
